<?php

namespace App\Models\Mining;

use App\Common\CommonModel;
use App\Models\Goods\Product;
use App\Models\Goods\UserProduct;
use App\Models\User;
use Dcat\Admin\Admin;
use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class UserMining extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'yida_user_mining';

    /**
     * 状态：待结算
     */
    const STATUS_UNPAY = 1 ;


    /**
     * 状态：已结算
     */
    const STATUS_PAYED = 2;

    /**
     * 状态：作废
     */
    const STATUS_DELETED = 0;



    protected $appends = ['status_str'];

    protected static function booted()
    {
        parent::booted(); // TODO: Change the autogenerated stub
        //多表插入不能记录日志  不能使用观察者模式   要使用模型存储方法进行日志记录
        static::created(function ($userMining){
            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_insert'=>$userMining->toArray()];
            Log::channel("LaravelInsert")->error("添加操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"insert",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "添加操作",
                'table' => "yida_user_mining"
            ]);
        });
        static::updated(function ($userMining){
            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_update'=>$userMining->toArray()];
            Log::channel("LaravelUpdate")->error("更新操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"update",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "更新操作",
                'table' => "yida_user_mining"
            ]);
        });
        static::deleted(function ($userMining){

            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_delete'=>$userMining->toArray()];
            Log::channel("LaravelDelete")->error("删除操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"delete",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "删除操作",
                'table' => "yida_user_mining"
            ]);
        });
    }

    public function getStatusStrAttribute() {
        return ['作废','未结算','已结算','结算中'][$this->status];
    }


    public function user(){
        return $this->belongsTo(User::class,'user_id');
    }


    public function userProduct(){
        return $this->belongsTo(UserProduct::class,'order_id');
    }

}
